//SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

import { invoke } from "@tauri-apps/api/core";
import type { NodeContent } from "./board_draw_type";

export type EDGE_TYPE = number;
export const EDGE_TYPE_BEZIER: EDGE_TYPE = 0;
export const EDGE_TYPE_SMOOTH_STEP: EDGE_TYPE = 1;
export const EDGE_TYPE_STEP: EDGE_TYPE = 2;
export const EDGE_TYPE_STRAIGHT: EDGE_TYPE = 3;

export type DRAW_ORDER_BY = number;
export const DRAW_ORDER_BY_CREATE: DRAW_ORDER_BY = 0; //创建时间倒序
export const DRAW_ORDER_BY_CHAT: DRAW_ORDER_BY = 1;   //沟通时间倒序


export type BasicDrawInfo = {
    draw_name: string;
};

export type DrawInfo = {
    draw_id: string;
    board_id: string;
    basic_info: BasicDrawInfo;
    edit_member_user_id_list: string[];
    node_count: number;
    edge_count: number;
    create_time: number;
    update_time: number;
    last_chat_time: number;
};

export type NodeStyleInfo = {
    bg_color: string;
    border_width: string;
    border_color: string;
    font_color: string;
    layer: number;
};

export type ExDrawNodeInfo = {
    node_id: string;
    node_style: NodeStyleInfo;
    x: number;
    y: number;
    w: number;
    h: number;
    content: NodeContent;
    lock: boolean;
    create_time: number;
    update_time: number;
};

export type EdgeKey = {
    from_node_id: string;
    from_handle: string;
    to_node_id: string;
    to_handle: string;
};

export type BasicEdgeInfo = {
    edge_type: EDGE_TYPE;
    stroke_width: number;
    color: string;
    has_start_arrow: boolean;
    has_end_arrow: boolean;
    label: string;
    llm_link: boolean;
};

export type DrawEdgeInfo = {
    edge_key: EdgeKey;
    basic_info: BasicEdgeInfo;
    create_time: number;
    update_time: number;
};

export type CreateDrawRequest = {
    session_id: string;
    board_id: string;
    basic_info: BasicDrawInfo;
};

export type CreateDrawResponse = {
    code: number;
    err_msg: string;
    draw_id: string;
};

export type UpdateDrawBasicRequest = {
    session_id: string;
    board_id: string;
    draw_id: string;
    basic_info: BasicDrawInfo;
};

export type UpdateDrawBasicResponse = {
    code: number;
    err_msg: string;
};

export type UpdateDrawEditMemberRequest = {
    session_id: string;
    board_id: string;
    draw_id: string;
    edit_member_user_id_list: string[];
};

export type UpdateDrawEditMemberResponse = {
    code: number;
    err_msg: string;
};

export type RemoveDrawRequest = {
    session_id: string;
    board_id: string;
    draw_id: string;
};

export type RemoveDrawResponse = {
    code: number;
    err_msg: string;
};

export type ListDrawRequest = {
    session_id: string;
    board_id: string;
    order_by: DRAW_ORDER_BY;
    offset: number;
    limit: number;
};

export type ListDrawResponse = {
    code: number;
    err_msg: string;
    total_count: number;
    draw_list: DrawInfo[];
};

export type GetDrawRequest = {
    session_id: string;
    board_id: string;
    draw_id: string;
};

export type GetDrawResponse = {
    code: number;
    err_msg: string;
    draw: DrawInfo;
};

export type ExCreateNodeRequest = {
    session_id: string,
    board_id: string,
    draw_id: string,
    x: number;
    y: number;
    w: number;
    h: number;
    content: NodeContent;
    node_style: NodeStyleInfo;
};

export type CreateNodeResponse = {
    code: number;
    err_msg: string;
    node_id: string;
};

export type ExUpdateNodeContentRequest = {
    session_id: string;
    board_id: string;
    draw_id: string;
    node_id: string;
    content: NodeContent;
};

export type UpdateNodeContentResponse = {
    code: number;
    err_msg: string;
};

export type UpdateNodePositionRequest = {
    session_id: string;
    board_id: string;
    draw_id: string;
    node_id: string;
    x: number;
    y: number;
};

export type UpdateNodePositionResponse = {
    code: number;
    err_msg: string;
};

export type UpdateNodeSizeRequest = {
    session_id: string;
    board_id: string;
    draw_id: string;
    node_id: string;
    w: number;
    h: number;
};

export type UpdateNodeSizeResponse = {
    code: number;
    err_msg: string;
};

export type UpdateNodeStyleRequest = {
    session_id: string;
    board_id: string;
    draw_id: string;
    node_id: string;
    node_style: NodeStyleInfo;
};

export type UpdateNodeStyleResponse = {
    code: number;
    err_msg: string;
}


export type UpdateNodeLockRequest = {
    session_id: string;
    board_id: string;
    draw_id: string;
    node_id: string;
    lock: boolean;
};

export type UpdateNodeLockResponse = {
    code: number;
    err_msg: string;
};

export type ListNodeRequest = {
    session_id: string;
    board_id: string;
    draw_id: string;
};

export type ExListNodeResponse = {
    code: number;
    err_msg: string;
    node_list: ExDrawNodeInfo[],
};

export type GetNodeRequest = {
    session_id: string;
    board_id: string;
    draw_id: string;
    node_id: string;
};

export type ExGetNodeResponse = {
    code: number;
    err_msg: string;
    node: ExDrawNodeInfo;
};

export type RemoveNodeRequest = {
    session_id: string;
    board_id: string;
    draw_id: string;
    node_id: string;
};

export type RemoveNodeResponse = {
    code: number;
    err_msg: string;
};

export type CreateEdgeRequest = {
    session_id: string;
    board_id: string;
    draw_id: string;
    edge_key: EdgeKey;
    basic_info: BasicEdgeInfo;
};

export type CreateEdgeResponse = {
    code: number;
    err_msg: string;
};

export type UpdateEdgeBasicRequest = {
    session_id: string;
    board_id: string;
    draw_id: string;
    edge_key: EdgeKey;
    basic_info: BasicEdgeInfo;
};

export type UpdateEdgeBasicResponse = {
    code: number;
    err_msg: string;
};

export type ListEdgeRequest = {
    session_id: string;
    board_id: string;
    draw_id: string;
};

export type ListEdgeResponse = {
    code: number;
    err_msg: string;
    edge_list: DrawEdgeInfo[];
};

export type GetEdgeRequest = {
    session_id: string;
    board_id: string;
    draw_id: string;
    edge_key: EdgeKey;
};

export type GetEdgeResponse = {
    code: number;
    err_msg: string;
    edge: DrawEdgeInfo;
};

export type RemoveEdgeRequest = {
    session_id: string;
    board_id: string;
    draw_id: string;
    edge_key: EdgeKey;
};

export type RemoveEdgeResponse = {
    code: number;
    err_msg: string;
};

export type RemoveEdgeByHandleRequest = {
    session_id: string;
    board_id: string;
    draw_id: string;
    node_id: string;
    handle_list: string[];
};

export type RemoveEdgeByHandleResponse = {
    code: number;
    err_msg: string;
};

export type ReportMousePositionRequest = {
    session_id: string;
    board_id: string;
    draw_id: string;
    x: number;
    y: number;
}

export type ReportMousePositionResponse = {
    code: number;
    err_msg: string;
}

// 创建画板
export async function create_draw(request: CreateDrawRequest): Promise<CreateDrawResponse> {
    const cmd = 'board_draw_api_cmds_create_draw';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<CreateDrawResponse>(cmd, {
        request,
    });
}

// 更新画板基本信息
export async function update_draw_basic(request: UpdateDrawBasicRequest): Promise<UpdateDrawBasicResponse> {
    const cmd = 'board_draw_api_cmds_update_draw_basic';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<UpdateDrawBasicResponse>(cmd, {
        request,
    });
}

// 更新画稿编辑人员
export async function update_draw_edit_member(request: UpdateDrawEditMemberRequest): Promise<UpdateDrawEditMemberResponse> {
    const cmd = 'board_draw_api_cmds_update_draw_edit_member';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<UpdateDrawEditMemberResponse>(cmd, {
        request,
    });
}

// 删除画板
export async function remove_draw(request: RemoveDrawRequest): Promise<RemoveDrawResponse> {
    const cmd = 'board_draw_api_cmds_remove_draw';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<RemoveDrawResponse>(cmd, {
        request,
    });
}

// 列出画板
export async function list_draw(request: ListDrawRequest): Promise<ListDrawResponse> {
    const cmd = 'board_draw_api_cmds_list_draw';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<ListDrawResponse>(cmd, {
        request,
    });
}

// 获取单个画板
export async function get_draw(request: GetDrawRequest): Promise<GetDrawResponse> {
    const cmd = 'board_draw_api_cmds_get_draw';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<GetDrawResponse>(cmd, {
        request,
    });
}

// 创建节点
export async function create_node(request: ExCreateNodeRequest): Promise<CreateNodeResponse> {
    const cmd = 'board_draw_api_cmds_create_node';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<CreateNodeResponse>(cmd, {
        request,
    });
}

// 更新节点内容
export async function update_node_content(request: ExUpdateNodeContentRequest): Promise<UpdateNodeContentResponse> {
    const cmd = 'board_draw_api_cmds_update_node_content';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<UpdateNodeContentResponse>(cmd, {
        request,
    });
}

// 更新节点位置
export async function update_node_position(request: UpdateNodePositionRequest): Promise<UpdateNodePositionResponse> {
    const cmd = 'board_draw_api_cmds_update_node_position';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<UpdateNodePositionResponse>(cmd, {
        request,
    });
}

// 更新节点大小
export async function update_node_size(request: UpdateNodeSizeRequest): Promise<UpdateNodeSizeResponse> {
    const cmd = 'board_draw_api_cmds_update_node_size';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<UpdateNodeSizeResponse>(cmd, {
        request,
    });
}

// 更新节点样式
export async function update_node_style(request: UpdateNodeStyleRequest): Promise<UpdateNodeStyleResponse> {
    const cmd = 'board_draw_api_cmds_update_node_style';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<UpdateNodeStyleResponse>(cmd, {
        request,
    });
}

// 更新节点锁定状态
export async function update_node_lock(request: UpdateNodeLockRequest): Promise<UpdateNodeLockResponse> {
    const cmd = 'board_draw_api_cmds_update_node_lock';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<UpdateNodeLockResponse>(cmd, {
        request,
    });
}

// 列出节点
export async function list_node(request: ListNodeRequest): Promise<ExListNodeResponse> {
    const cmd = 'board_draw_api_cmds_list_node';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<ExListNodeResponse>(cmd, {
        request,
    });
}

// 获取单个节点
export async function get_node(request: GetNodeRequest): Promise<ExGetNodeResponse> {
    const cmd = 'board_draw_api_cmds_get_node';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<ExGetNodeResponse>(cmd, {
        request,
    });
}

// 删除节点
export async function remove_node(request: RemoveNodeRequest): Promise<RemoveNodeResponse> {
    const cmd = 'board_draw_api_cmds_remove_node';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<RemoveNodeResponse>(cmd, {
        request,
    });
}

// 创建连线
export async function create_edge(request: CreateEdgeRequest): Promise<CreateEdgeResponse> {
    const cmd = 'board_draw_api_cmds_create_edge';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<CreateEdgeResponse>(cmd, {
        request,
    });
}

// 更新连线基本信息
export async function update_edge_basic(request: UpdateEdgeBasicRequest): Promise<UpdateEdgeBasicResponse> {
    const cmd = 'board_draw_api_cmds_update_edge_basic';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<UpdateEdgeBasicResponse>(cmd, {
        request,
    });
}

// 列出连线
export async function list_edge(request: ListEdgeRequest): Promise<ListEdgeResponse> {
    const cmd = 'board_draw_api_cmds_list_edge';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<ListEdgeResponse>(cmd, {
        request,
    });
}

// 获取单个连线
export async function get_edge(request: GetEdgeRequest): Promise<GetEdgeResponse> {
    const cmd = 'board_draw_api_cmds_get_edge';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<GetEdgeResponse>(cmd, {
        request,
    });
}

// 删除连线
export async function remove_edge(request: RemoveEdgeRequest): Promise<RemoveEdgeResponse> {
    const cmd = 'board_draw_api_cmds_remove_edge';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<RemoveEdgeResponse>(cmd, {
        request,
    });
}

// 按连接点删除连线
export async function remove_edge_by_handle(request: RemoveEdgeByHandleRequest): Promise<RemoveEdgeByHandleResponse> {
    const cmd = 'board_draw_api_cmds_remove_edge_by_handle';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<RemoveEdgeByHandleResponse>(cmd, {
        request,
    });
}

// 上报鼠标位置
export async function report_mouse_position(request: ReportMousePositionRequest): Promise<RemoveEdgeResponse> {
    const cmd = 'board_draw_api_cmds_report_mouse_position';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<ReportMousePositionResponse>(cmd, {
        request,
    });
}